package com.zzxx.service.impl;

import com.zzxx.entity.*;
import com.zzxx.exceptions.ManagerLoginException;
import com.zzxx.exceptions.ManagerRegisterException;
import com.zzxx.mapper.*;
import com.zzxx.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ManagerServiceImpl implements ManagerService {

	@Autowired
	ManagerMapper managerMapper;

	@Autowired(required = false)
	HttpServletRequest request;

	@Autowired
	private ManagerQrMapper managerQrMapper;

	@Autowired
	private ManagerPowerMapper managerPowerMapper;

	@Autowired
	private GymMapper gymMapper;

	@Autowired
	private ActivityMapper activityMapper;

	@Override
	public Manager login(String name, String password) throws ManagerLoginException {
		if ("".equals(name)) throw new ManagerLoginException("账号为空，请输入账号！");
		if ("".equals(password)) throw new ManagerLoginException("密码为空，请输入密码！");
		ManagerExample example = new ManagerExample();
		example.createCriteria().andManagernameEqualTo(name);
		List<Manager> manager = managerMapper.selectByExample(example);
		if (manager.size()==1){
			if (password.equals(manager.get(0).getPassword())) return manager.get(0);
			throw new ManagerLoginException("密码不正确!请重新输入密码！");
		}
		throw new ManagerLoginException("登陆失败，此用户不存在！");
	}

	@Override
	public void register(Manager manager) throws ManagerRegisterException {
		String checkCode = manager.getIntroduce();
		manager.setId(UUID.randomUUID().toString().replaceAll("-",""));
		Object _checkCode = request.getSession().getAttribute("checkCode");
		//测试用Test，后期要删掉manager.setIntroduce(null);
		if (!checkCode.equals(_checkCode)&&!checkCode.equals("Test")){
			throw new ManagerRegisterException("验证码输入有误");
		}
		try{
			managerMapper.insert(manager);
		}catch (Exception e){
			throw new ManagerRegisterException("注册失败!");
		}
	}

	@Override
	public List<ManagerQr> getWriteOffList(String managerId) {
		ManagerQrExample managerQrExample = new ManagerQrExample();
		managerQrExample.setOrderByClause("time DESC");
		managerQrExample.createCriteria().andManageridEqualTo(managerId);
		List<ManagerQr> managerQrs = managerQrMapper.selectByExample(managerQrExample);
		return managerQrs;
	}

	@Override
	public List<GymAndPower> findGymListById(String id) {
		ManagerPowerExample example = new ManagerPowerExample();
		example.createCriteria().andManageridEqualTo(id);
		List<ManagerPower> managerPowers = managerPowerMapper.selectByExample(example);
		List<GymAndPower> list = new ArrayList<>();
		managerPowers.forEach(n->{
			if (n.getPowertype()==0){
				Gym gym = gymMapper.selectByPrimaryKey(n.getPowerid());
				GymAndPower gap = new GymAndPower();
				String powerName = "";
				switch (n.getPowerlevel()){
					case 3: powerName += "可给店员授权、";
					case 2: powerName += "可以修改或发布售票信息、";
					case 1:	powerName += "可以核销票";
				}
				gap.setGym(gym);
				gap.setManagerPower(n);
				gap.setPowerName(powerName);
				list.add(gap);
			}
		});
		return list;
	}

	@Override
	public GymAndPower findGapById(String id) {
		Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
		if (loginManager==null) return null;
		GymAndPower gap = new GymAndPower();
		ManagerPower managerPower = managerPowerMapper.selectByPrimaryKey(id);
		if (managerPower==null|| !managerPower.getManagerid().equals(loginManager.getId())) return null;
		gap.setManagerPower(managerPower);
		gap.setGym(gymMapper.selectByPrimaryKey(managerPower.getPowerid()));
		return gap;
	}

	@Override
	public List<GymAndPower> findActivityListById(String id) {
		ManagerPowerExample example = new ManagerPowerExample();
		example.createCriteria().andManageridEqualTo(id).andPowertypeEqualTo(1);
		List<ManagerPower> managerPowers = managerPowerMapper.selectByExample(example);
		List<GymAndPower> list = new ArrayList<>();
		managerPowers.forEach(n->{
			if (n.getPowertype()==1){
				Activity activity = activityMapper.selectByPrimaryKey(n.getPowerid());
				GymAndPower gap = new GymAndPower();
				String powerName = "";
				switch (n.getPowerlevel()){
					case 3: powerName += "可给店员授权、";
					case 2: powerName += "可以修改或发布售票信息、";
					case 1:	powerName += "可以核销票";
				}
				gap.setActivity(activity);
				gap.setManagerPower(n);
				gap.setPowerName(powerName);
				list.add(gap);
			}
		});
		return list;
	}

	@Override
	public GymAndPower findAmpById(String id) {
		Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
		if (loginManager==null) return null;
		GymAndPower gap = new GymAndPower();
		ManagerPower managerPower = managerPowerMapper.selectByPrimaryKey(id);
		if (managerPower==null|| !managerPower.getManagerid().equals(loginManager.getId())) return null;
		gap.setManagerPower(managerPower);
		gap.setActivity(activityMapper.selectByPrimaryKey(managerPower.getPowerid()));
		return gap;
	}
}
